package com.mikey.emoji.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.mikey.emoji.common.ApiRestResponse;
import com.mikey.emoji.exception.YunzhiMallExceptionEnum;
import com.mikey.emoji.model.pojo.Paper;
import com.mikey.emoji.model.pojo.Tag;
import com.mikey.emoji.model.request.AddTagReq;
import com.mikey.emoji.model.request.DeleteTagReq;
import com.mikey.emoji.model.request.SelectTagReq;
import com.mikey.emoji.service.PaperService;
import com.mikey.emoji.service.TagService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author gali
 * @date 2022/4/20 0:15
 */
@RestController
@RequestMapping("/tag")
@Slf4j
public class TagController {
    @Resource
    private TagService tagService;
    @Resource
    private PaperService paperService;

    /**
     * 增加标签
     * @param addTagReq
     * @return
     */
    @PostMapping("/add")
    public ApiRestResponse addTag(@RequestBody @Valid AddTagReq addTagReq){
        Tag tag = new Tag();
        BeanUtils.copyProperties(addTagReq,tag);
        boolean save = tagService.save(tag);
        if (save){
            return ApiRestResponse.success();
        }
        return ApiRestResponse.error(YunzhiMallExceptionEnum.ADD_ERROR);
    }

    /**
     * 查询全部标签
     * @return
     */
    @GetMapping("/findAll")
    public ApiRestResponse findAll(){
        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
        List<Tag> list = tagService.list(tagQueryWrapper);
        if (list!=null){
            return ApiRestResponse.success(list);
        }
        return ApiRestResponse.error(YunzhiMallExceptionEnum.SELECT_ERROR);
    }

    /**
     * 通过标签名查询表情
     * @param selectTagReq
     * @return
     */
    @PostMapping("/findByTag")
    public ApiRestResponse findByTag(@RequestBody @Valid SelectTagReq selectTagReq){
         String name = selectTagReq.getName();
         //生成条件构造器
        QueryWrapper<Paper> tagQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isBlank(selectTagReq.getName())){
            return ApiRestResponse.error(YunzhiMallExceptionEnum.REQUEST_PARAM_ERROR);
        }
        tagQueryWrapper.like("tags", name);
        tagQueryWrapper.orderByAsc("createTime");
        tagQueryWrapper.eq("reviewStatus",1);
        List<Paper> list = paperService.list(tagQueryWrapper);
        if (list.size()>0){
            return ApiRestResponse.success(list);
        }
        return ApiRestResponse.error(YunzhiMallExceptionEnum.SELECT_ERROR);
    }

    /**
     * 删除标签
     * @param deleteTagReq
     * @return
     */
    @PostMapping("/delete")
    public ApiRestResponse deleteTag(@RequestBody @Valid DeleteTagReq deleteTagReq){
        QueryWrapper<Tag> tagQueryWrapper = new QueryWrapper<>();
        tagQueryWrapper.eq("id",deleteTagReq.getId());
        boolean remove = tagService.remove(tagQueryWrapper);
        if (remove){
            return ApiRestResponse.success();
        }
        return ApiRestResponse.error(YunzhiMallExceptionEnum.DELETE_ERROR);
    }
}
